package business;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

public class MMSeqReader {
	private BufferedReader reader;
	String[] headers;
	
	public MMSeqReader (String filepath)
	{
		try {
			reader= new BufferedReader(new FileReader( new File (filepath)));
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		}
		String firstline = null;
		try {
			firstline = reader.readLine();
		} catch (IOException e) {
			e.printStackTrace();
		}
		headers = firstline.split("\t"); //stores headers of mmseq File
	}
	
	public String[] readLine ()
	{
		String line = null;
		try {
			line = reader.readLine();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		if(line == null)
			return null;
		String[] values = line.split("\t");
		return values;
	}
	
	public HashMap<String, String[]> readMMSeq () //HashMap with a key of transcript name corresponding to an array of values
	{
		HashMap<String, String[]> lines = new HashMap<String, String[]>();
		String[] line = readLine(); //first value is name of transcript
		while(line != null)
		{
			lines.put(line[0], line);
			line = readLine();
		}
		return lines;
	}
	
	public String[] getHeaders ()
	{
		return headers;
	}
	
	public List<String> getSampleNames() //returns an indexed list of sample names to search
	{
		List<String> sampleNames = new ArrayList<String>();
		for(int k = 0; k < headers.length; k++)
		{
			if(k % 8 == 0) //each sampleName has 8 columns
				sampleNames.add(headers[k].split("_")[0].substring(1));
		}
		return sampleNames;
	}
	
	public static void main (String[] args)
	{
		//MMSeqReader m = new MMSeqReader("C:\\Users\\Lucas\\Desktop\\demo\\demo\\brain.heart.adipose.lung.mmseq");
		//for(String s: m.getSampleNames())
		//	System.out.println(s);
	}
}
